<!DOCTYPE html>
<html>
<head>
	<title>oss-upload</title>
</head>
<body>
<input type="file" id="fileName">sdf 
    <!-- <img id="img" src="./bw1.jpg" alt=""> -->
   <button onclick="upload()" >上传</button>
   <script src="http://gosspublic.alicdn.com/aliyun-oss-sdk-4.4.4.min.js"></script>


<script src="http://gosspublic.alicdn.com/aliyun-oss-sdk-4.4.4.min.js"></script>

  






 <script type="text/javascript">
    var client = new OSS.Wrapper({
        region: "oss-cn-chengdu", //阿里云获取
        accessKeyId: "LTAI4FeNF9DdZksiF1UPDzsV",
        accessKeySecret: "xDqI8YPVaTW7fqiUqtoLisPPrhX3hS",
        bucket: "house-sell"  //要存储的目录名
    });

    function upload() {
        var file = document.getElementById("fileName").files[0];
        var val = document.getElementById("fileName").value;
        // 取出文件后缀名
        var suffix = val.substr(val.indexOf("."));
        var obj = timestamp();
        // var storeAs = 'upload-file/' + "/" + obj + suffix;
        var storeAs = obj + suffix;
        client.multipartUpload(storeAs, file).then(function (result) {
            var url = result.res.requestUrls[0];
            var length = url.lastIndexOf('?');
            var imgUrl = url.substr(0,length);//文件最终路径
            // $("#img").attr("src",url.substr(0,length))
            console.log(url.substr(0,length))
        }).catch(function (err) {
            console.log(err);
        });
    }
    /**
     * 生成文件名
     * @returns
     */
    function timestamp() {
        var time = new Date();
        var y = time.getFullYear();
        var m = time.getMonth() + 1;
        var d = time.getDate();
        var h = time.getHours();
        var mm = time.getMinutes();
        var s = time.getSeconds();
        return "" + y + add0(m) + add0(d) + add0(h) + add0(mm) + add0(s);
    }

    function add0(m) {
        return m < 10 ? '0' + m : m;
    }
</script>
</body>
</html>
